package com.btex.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.btex.app.tools.CookieTool;
import com.btex.app.tools.LogUtil;
import com.btex.app.tools.MicroAVLIDialog;
import com.btex.app.tools.Util;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by wcx on 16/3/25.
 */
public class TradeRegistPsw extends BaseActivity {

    private static final java.lang.String TAG = "TradeRegistPsw";
    private EditText etPsw;
    private EditText etRePsw;
    private Button btnConfirm;
    private PreferencesCookieStore cookiestore;
    private MicroAVLIDialog mMicroAVLIDialog;

    private void assignViews() {
        etPsw = (EditText) findViewById(R.id.etPsw);
        etRePsw = (EditText) findViewById(R.id.etRePsw);
        btnConfirm = (Button) findViewById(R.id.btnConfirm);
        btnConfirm.setOnClickListener(this);
        cookiestore = CookieTool.getCookieStore(cxt, sp);

        etPsw.addTextChangedListener(new MyWatcher());
        etRePsw.addTextChangedListener(new MyWatcher());
        mMicroAVLIDialog = new MicroAVLIDialog(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trade_psw);
        assignViews();
        initData();
    }

    @Override
    public void setData() {
        tv_title.setText(getString(R.string.act_registPsw_title));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btnConfirm:
                mMicroAVLIDialog.show();
                toReistPsw(etPsw.getText().toString(), etRePsw.getText().toString());
                break;
        }
    }

    private void toReistPsw(String tradePsw, String reTradePsw) {
//        reTradePswif(tradePsw.length()<8&&reTradePsw.length()<8){
//            Toast.makeText(cxt,getString(R.string.ps))
//        }


        String isEngilshs;

        boolean isEngilsh = sp.getBoolean("isEngilsh", false);
        if (isEngilsh) {
            isEngilshs = "en";
        } else {
            isEngilshs = "zh_cn";
        }

        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_TRADE_PSW, tradePsw);
        params.addBodyParameter(Config.KEY_RE_TRADE_PSW, reTradePsw);
        params.addBodyParameter("lang", isEngilshs);
        params.addHeader("myclient", "android");
        new HttpUtils().configCookieStore(cookiestore).send(HttpRequest.HttpMethod.POST, Config.URL_CHANGE_TRADE_PSW, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mMicroAVLIDialog.dismiss();
                String result = responseInfo.result.toString();
                LogUtil.i(TAG, "result:" + result);
                try {
                    JSONObject json = new JSONObject(result);
                    if ("1".equals(json.getString(Config.KEY_CODE))) {
                        LogUtil.i(TAG, "cheng gong");
                        Intent i = new Intent(cxt, ConfirmIdentityActivity.class);
                        startActivity(i);
                        finish();
                    } else if ("-1".equals(json.getString(Config.KEY_CODE))) {

//                        SnackbarShow(json.getString("info"));
//                        Toast.makeText(cxt, json.getJSONObject("info").getString("zh_cn"), Toast.LENGTH_SHORT).show();
                        Util.showSnachBar(TradeRegistPsw.this, getResources().getColor(R.color.red),json.getString("info"));
                    }
                } catch (JSONException e) {
                    LogUtil.i(TAG, e.toString());
                }

            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    private class MyWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            if (!TextUtils.isEmpty(etPsw.getText()) && !TextUtils.isEmpty(etRePsw.getText())) {
                btnConfirm.setBackgroundResource(R.drawable.selector_btn_app_color);
                btnConfirm.setClickable(true);
                btnConfirm.setTextColor(getResources().getColor(R.color.white));
            } else {
                btnConfirm.setBackgroundResource(R.color.bg_gray);
                btnConfirm.setClickable(false);
                btnConfirm.setTextColor(getResources().getColor(R.color.tv_gray));
            }
        }
    }


//    private void SnackbarShow(String ms) {
//
//        Snackbar snackbar = ShortSnackbar(etPsw, ms, Color.parseColor("#ffffff"), Color.parseColor("#272736"));
//        snackbar.show();
//    }
//
//    public static Snackbar ShortSnackbar(View view, String message, int messageColor, int backgroundColor) {
//        Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
//        setSnackbarColor(snackbar, messageColor, backgroundColor);
//        return snackbar;
//    }
//
//    /**
//     * 设置Snackbar文字和背景颜色
//     *
//     * @param snackbar
//     * @param messageColor
//     * @param backgroundColor
//     */
//    public static void setSnackbarColor(Snackbar snackbar, int messageColor, int backgroundColor) {
//        View view = snackbar.getView();
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            TextView message = (TextView) snackbar.getView().findViewById(R.id.snackbar_text);
//            //View.setTextAlignment需要SDK>=17
//            message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
//            message.setGravity(Gravity.CENTER);
//        }
//
//        if (view != null) {
//            view.setBackgroundColor(backgroundColor);
//            ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(messageColor);
//        }
//    }
}
